<template>
  <el-dialog title="新增工单" width="600px" :visible="dialogFormVisible" @close="closeForm">
    <el-form ref="form" :model="rowData" label-width="80px">
      <el-form-item label="设备编号" prop="innerCode">
        <el-input v-model="rowData.innerCode" />
      </el-form-item>
      <el-form-item v-if="typeList.length > 0" label="工单类型" prop="productTypeId">
        <el-select v-model="rowData.productTypeId" placeholder="请选择" style="width:100%">
          <el-option
            v-for="item in typeList"
            :key="item.typeId"
            :label="item.typeName || ''"
            :value="item.typeId"
          />
        </el-select>
      </el-form-item>
      <el-form-item v-if="repairerList.length > 0" label="运维人员" prop="userId">
        <el-select v-model="rowData.userId" placeholder="请选择" style="width:100%">
          <el-option
            v-for="item in repairerList"
            :key="item.userId"
            :label="item.userName"
            :value="item.userId"
          />
        </el-select>
      </el-form-item>
      <el-form-item v-if="repairerList.length === 0" label="运维人员" prop="userId">
        <el-select v-model="rowData.userId" placeholder="请选择" style="width:100%">
          <el-option
            label="无数据"
            value=""
          />
        </el-select>
      </el-form-item>
      <el-form-item label="备注" prop="desc">
        <el-input v-model="rowData.desc" type="textarea" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="closeForm">取 消</el-button>
      <el-button type="danger" @click="submitForm">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { mapState, mapMutations } from 'vuex'
import { getTypeList, getRepairerList, createTask } from '@/api/table'
import { Message } from 'element-ui'
export default {
  name: 'AddDialog',
  data() {
    return {
      typeList: [],
      repairerList: [] || [
        { userId: 6, userName: '运维刘俊杰' },
        { userId: 7, userName: '运维金瀚漠' },
        { userId: 11, userName: '运维汪阳羽' }
      ]
    }
  },
  computed: {
    ...mapState('maintenance', ['dialogFormVisible', 'rowData'])
  },
  watch: {
    rowData(newVal) {
      // 拿数据
      this.getRepairerList(newVal)
    },
    dialogFormVisible: {
      handler(newVal) {
        if (!newVal) {
          console.log(this.$refs)
          // 恢复初始值
          this.$refs.form.resetFields()
          // 清空表单数据
          this.clearRowDate()
        }
      }
    }
  },
  created() {
    // 拿数据
    this.getTypes()
  },
  methods: {
    ...mapMutations('maintenance', ['closeForm', 'clearRowDate']),
    async getTypes() {
      try {
        const res = await getTypeList()
        this.typeList = res
      } catch (error) {
        throw new Error(error)
      }
    },
    async getRepairerList(newVal) {
      try {
        const list = await getRepairerList(newVal.innerCode)
        // console.log(list)
        this.repairerList = list
      } catch (error) {
        throw new Error(error)
      }
    },
    async submitForm() {
      try {
        // 整理参数
        const { assignorId, createType, desc, innerCode, productTypeId: productType, userId } = this.rowData
        const data = { assignorId, createType, desc, innerCode, productType, userId }
        data.details = []
        // 发送请求
        await createTask(data)
        Message({
          message: '创建成功',
          type: 'success'
        })
      } catch (error) {
        Message({
          message: error.message,
          type: 'error'
        })
      } finally {
        this.closeForm()
      }
    }
  }
}
</script>

<style>

</style>
